/**
 * Created by alice on 2017/8/17 0001
 */
;-function (window) {
    "use strict";
    //TODO 预设变量写这里，全局变量写在base.js 里面 使用 r. 调用
    var ready = {
        layer: null,
        page_size: 12
    };
    //TODO 初始化的方法写这里面
    var init = function () {
        init_ready();//加载预设变量
        init_event();

        function init_event() {//TODO 初始化页面事件
            Class.page(1, Math.ceil(count ? (count / 12) : 1), Class.select_favorite);
            Class.init_favorite(result);
            var select_child_courts = $("#selectAllChildCourts");
            select_child_courts.change(function () {
                Class.select_all_court_room(select_child_courts.val());
            });
            $('#startTime').datetimepicker({
                bootcssVer: 3,
                minView: "month", //选择日期后，不会再跳转去选择时分秒
                weekStart: "1",
                format: "yyyy-mm-dd", //选择日期后，文本框显示的日期格式
                language: 'zh-CN', //汉化
                autoclose: true //选择日期后自动关闭
            }).on('changeDate', function (ev) {
                // console.log(ev.date.valueOf());
            });
            $('#endTime').datetimepicker({
                bootcssVer: 3,
                minView: "month", //选择日期后，不会再跳转去选择时分秒
                weekStart: "1",
                format: "yyyy-mm-dd", //选择日期后，文本框显示的日期格式
                language: 'zh-CN', //汉化
                autoclose: true //选择日期后自动关闭
            }).on('changeDate', function (ev) {
                // console.log(ev.date.valueOf());
            });

            $("#conditionBtn").click(function () {
                $("#loading").show();
                Class.select_favorite(1);
            });
        }

        function init_ready() {//TODO 初始化预设值
            layui.use('layer', function () {
                ready.layer = layui.layer;
            });
            var select_child_courts = $("#selectAllChildCourts");
            select_child_courts.append("<option value=''>所有法院</option>");
            $.each(courts, function (i, n) {
                select_child_courts.append("<option value='" + n.uid + "'>" + n.name + "</option>");
            });
            Class.select_all_court_room(-1);
            var case_type = $("#caseTypeName").empty();
            case_type.append("<option value=''>所有类型</option>");
            console.log(caseType);
            $.each(caseType, function (index, elem) {
                case_type.append("<option value='" + elem.uid + "'>" + elem.name + "</option>");
            });
        }
    };
    //TODO 内部调用的方法写这里
    var Class = {
        select_all_court_room: function (court_uid) {
            var court_room = $("#allCourtRooms").empty();
            court_room.append("<option value=''>" + "所有法庭" + "</option>");
            $.each(courtrooms, function (i, elem) {
                if (elem.courtUid == court_uid && elem.active == 1) {
                    court_room.append("<option value='" + elem.uid + "'>" + elem.name + "</option>");
                }
            });
            var select_judge = $("#allJudges").empty();
            select_judge.append("<option value=''>所有法官</option>");
            $.each(judgeList, function (index, elem) {
                if (elem.courtUid == court_uid) {
                    select_judge.append("<option value='" + elem.uid + "'>" + elem.name + "</option>");
                }
            });
            var select_secretary = $("#allSecretary").empty();
            select_secretary.append("<option value=''>所有书记员</option>");
            $.each(secretaryList, function (index, elem) {
                if (elem.courtUid == court_uid) {
                    select_secretary.append("<option value='" + elem.uid + "'>" + elem.name + "</option>");
                }
            });
        },
        select_favorite: function (page) {
            var courtUid = $("#selectAllChildCourts").val();
            var caseno = $("#caseno").val();
            var caseName = $("#caseName").val();
            var caseTypeName = $("#caseTypeName").val();
            var courtRoom = $("#allCourtRooms").val();
            var judge = $("#allJudges").val();
            var secretary = $("#allSecretary").val();
            var startTime = $("#startTime").find("input").val();
            var endTime = $("#endTime").find("input").val();
            $.get(r.base_path + 'favorite/selectFavorite', {
                courtUid: courtUid,
                archives: caseno,
                caseName: caseName,
                type: caseTypeName,
                courtRoom: courtRoom,
                judgeId: judge,
                secretaryId: secretary,
                startDate: startTime,
                endDate: endTime,
                status: 0,
                review: 1,
                page: page,
                pageSize: ready.page_size
            }, function (result) {
                Class.init_favorite(result.data.result);
                Class.page(page, Math.ceil(result.data.count / 12), Class.select_favorite);
            });
        },
        init_favorite: function (data) {
            var row_no_padding = $(".row-no-padding").empty();
            $(data).each(function (index, elem) {
                var caseType = '';
                switch (elem.caseType) {
                    case 'a1':
                        caseType = '刑事';
                        break;
                    case 'a2':
                        caseType = '民事';
                        break;
                    case 'a3':
                        caseType = '执行';
                        break;
                    case 'a4':
                        caseType = '赔偿';
                        break;
                    case 'a5':
                        caseType = '行政';
                        break;
                    case 'a6':
                        caseType = '信访';
                        break;
                    case 'a7':
                        caseType = '其他';
                        break;
                    default:
                        break;
                }
                var randomNum = parseInt(6 * Math.random() + 1);//随机出1到picNum的整数
                var href = r.base_path + "system_monitor/panel_play?planUid=" + elem.uid + "&vlcCourtUid=" + elem.courtUid + '&courtUid=' + r.court_uid;
                var html = '<div class="col-md-2 col-xs-6">' +
                    '<div class="more-reviewing-item">' +
                    '<a href="' + href + '">' +
                    '<div class="more-reviewing-item-img-panel">' +
                    '<p class="more-reviewing-item-img-panel-top">' + (elem.courtroomName || '未知') + '</p>' +
                    '<img src="' + r.base_path + "assets/img/more_court_test" + randomNum + ".jpg" + '" alt="法院">' +
                    '<p class="more-reviewing-item-img-panel-bottom">' + caseType + '</p>' +
                    '</div>' +
                    '<p>' + elem.start + '</p>' +
                    '<p>' + (elem.archives || '未知') + '</p>' +
                    '<p class="more-reviewing-item-depiction">' +
                    '<i class="fa fa-thumb-tack status-top " title="取消收藏" id="' + elem.uid + '..' + elem.courtUid + '"></i>' +
                    '<span>' + (elem.name || "&nbsp;") + '</span>' +
                    '</p>' +
                    '</a>' +
                    '</div>' +
                    '</div>';
                row_no_padding.append(html);
            });
            $(".fa-thumb-tack").click(function (e) {
                e.preventDefault();
                var cid = this.id.split('..');
                var uid = cid[0];
                layui.use(['layer', 'form'], function () {
                    var layer = layui.layer;
                    layer.confirm('确定取消收藏吗？', {
                        btn: ['是的', '取消'] //按钮
                    }, function () {
                        $.post(r.base_path + "favorite/unlike", {
                            planUid: uid
                        }, function (result) {
                            layer.msg(result.message);
                            Class.select_favorite(1);
                        }, 'json')
                    }, function () {

                    });
                });
            });
            row_no_padding.show();
        },
        page: function (now, totalPages, callback) {
            base.pagination('#_page', {
                currentPage: now,
                totalPages: totalPages||1,
                onPageClicked: function (page) {
                    callback(page);
                }
            });
        }
    };
    init();
    //TODO 对外公开的方法写在这里
    window.p = {};
}(window);